﻿using System;
using System.Linq;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
    
    public class MenuController : Controller
    {
        CMSMVC4Entities db = new CMSMVC4Entities();
        CommandSql cm = new CommandSql();

        [Authorize(Roles = "Admin,Menu")]
        public ActionResult Sort()
        {
            //ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services").Rows[0]["Parrent"];
            string curte = cm.GetLanguage();
            var data = db.MenuLangs.OrderBy(x => x.Menu.Sort).Where(x => x.Menu.Area == MenuArea.Home && x.LanguageCode.Equals(curte)).ToList();
            return View(data);
        }

        [Authorize(Roles = "Admin,Menu")]
        [HttpPost]
        public JsonResult Sort(ob[] aro)
        {
            if (aro != null)
            {
                for (int i = 0; i < aro.Length; i++)
                {
                    int id = Convert.ToInt32(aro[i].id);
                    Menu ct = db.Menus.FirstOrDefault(x => x.Id == id);
                    if (ct != null)
                    {
                        int sort = Convert.ToInt32(aro[i].sort);
                        ct.Sort = sort;
                        db.SaveChanges();
                    }
                }
                return Json("Lưu dữ liệu thành công", JsonRequestBehavior.AllowGet);
            }
            return Json("Lỗi hệ thống không lưu được dữ liệu", JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "Admin,Menu")]
        public ActionResult Index(string mess)
        {
            Menu gioithieu = db.Menus.FirstOrDefault(x => x.Type == MenuType.GioiThieu && x.Area == MenuArea.Home);
            if (gioithieu == null)
            {
                Menu mn = new Menu();
                mn.Area = MenuArea.Home;
                mn.Type = MenuType.GioiThieu;
                mn.Parrent = 0;
                mn.Sort = 1;
                mn.Url = "/GioiThieu";
                db.Menus.AddObject(mn);
                db.SaveChanges();
                gioithieu = db.Menus.FirstOrDefault(x => x.Type == MenuType.GioiThieu && x.Area == MenuArea.Home);
                MenuLang mlv = new MenuLang();
                mlv.MenuId = gioithieu.Id;
                mlv.Name = "GIỚI THIỆU";
                mlv.LanguageCode = "vi-vn";
                db.MenuLangs.AddObject(mlv);
                db.SaveChanges();
                MenuLang mle = new MenuLang();
                mle.MenuId = gioithieu.Id;
                mle.Name = "INTRO";
                mle.LanguageCode = "en-us";
                db.MenuLangs.AddObject(mle);
                db.SaveChanges();
            }
            Menu thietbi = db.Menus.FirstOrDefault(x => x.Type == MenuType.ThiteBi && x.Area == MenuArea.Home);
            if (thietbi == null)
            {
                Menu mn = new Menu();
                mn.Area = MenuArea.Home;
                mn.Type = MenuType.ThiteBi;
                mn.Parrent = 0;
                mn.Sort = 2;
                mn.Url = "/ThietBi";
                db.Menus.AddObject(mn);
                db.SaveChanges();
                thietbi = db.Menus.FirstOrDefault(x => x.Type == MenuType.ThiteBi && x.Area == MenuArea.Home);
                MenuLang mlv = new MenuLang();
                mlv.MenuId = thietbi.Id;
                mlv.Name = "THIẾT BỊ";
                mlv.LanguageCode = "vi-vn";
                db.MenuLangs.AddObject(mlv);
                db.SaveChanges();
                MenuLang mle = new MenuLang();
                mle.MenuId = thietbi.Id;
                mle.Name = "EQUIPMENTS";
                mle.LanguageCode = "en-us";
                db.MenuLangs.AddObject(mle);
                db.SaveChanges();
            }
            Menu dichvu = db.Menus.FirstOrDefault(x => x.Type == MenuType.DichVu && x.Area == MenuArea.Home);
            if (dichvu == null)
            {
                Menu mn = new Menu();
                mn.Area = MenuArea.Home;
                mn.Type = MenuType.DichVu;
                mn.Parrent = 0;
                mn.Sort = 3;
                mn.Url = "/DichVu";
                db.Menus.AddObject(mn);
                db.SaveChanges();
                dichvu = db.Menus.FirstOrDefault(x => x.Type == MenuType.DichVu && x.Area == MenuArea.Home);
                MenuLang mlv = new MenuLang();
                mlv.MenuId = dichvu.Id;
                mlv.Name = "DỊCH VỤ";
                mlv.LanguageCode = "vi-vn";
                db.MenuLangs.AddObject(mlv);
                db.SaveChanges();
                MenuLang mle = new MenuLang();
                mle.MenuId = dichvu.Id;
                mle.Name = "SERVICES";
                mle.LanguageCode = "en-us";
                db.MenuLangs.AddObject(mle);
                db.SaveChanges();
            }
            Menu tintuc = db.Menus.FirstOrDefault(x => x.Type == MenuType.TinTuc && x.Area == MenuArea.Home);
            if (tintuc == null)
            {
                Menu mn = new Menu();
                mn.Area = MenuArea.Home;
                mn.Type = MenuType.TinTuc;
                mn.Parrent = 0;
                mn.Sort = 4;
                mn.Url = "/TinTuc";
                db.Menus.AddObject(mn);
                db.SaveChanges();
                tintuc = db.Menus.FirstOrDefault(x => x.Type == MenuType.TinTuc && x.Area == MenuArea.Home);
                MenuLang mlv = new MenuLang();
                mlv.MenuId = tintuc.Id;
                mlv.Name = "TIN TỨC";
                mlv.LanguageCode = "vi-vn";
                db.MenuLangs.AddObject(mlv);
                db.SaveChanges();
                MenuLang mle = new MenuLang();
                mle.MenuId = tintuc.Id;
                mle.Name = "NEWS";
                mle.LanguageCode = "en-us";
                db.MenuLangs.AddObject(mle);
                db.SaveChanges();
            }
            Menu lienhe = db.Menus.FirstOrDefault(x => x.Type == MenuType.LienHe && x.Area == MenuArea.Home);
            if (lienhe == null)
            {
                Menu mn = new Menu();
                mn.Area = MenuArea.Home;
                mn.Type = MenuType.LienHe;
                mn.Parrent = 0;
                mn.Sort = 5;
                mn.Url = "/LienHe";
                db.Menus.AddObject(mn);
                db.SaveChanges();
                lienhe = db.Menus.FirstOrDefault(x => x.Type == MenuType.LienHe && x.Area == MenuArea.Home);
                MenuLang mlv = new MenuLang();
                mlv.MenuId = lienhe.Id;
                mlv.Name = "LIÊN HỆ";
                mlv.LanguageCode = "vi-vn";
                db.MenuLangs.AddObject(mlv);
                db.SaveChanges();
                MenuLang mle = new MenuLang();
                mle.MenuId = lienhe.Id;
                mle.Name = "CONTACT";
                mle.LanguageCode = "en-us";
                db.MenuLangs.AddObject(mle);
                db.SaveChanges();
            }
            Menu doitac = db.Menus.FirstOrDefault(x => x.Type == MenuType.DoiTac && x.Area == MenuArea.Home);
            if (doitac == null)
            {
                Menu mn = new Menu();
                mn.Area = MenuArea.Home;
                mn.Type = MenuType.DoiTac;
                mn.Parrent = 0;
                mn.Sort = 4;
                mn.Url = "/DoiTac";
                db.Menus.AddObject(mn);
                db.SaveChanges();
                doitac = db.Menus.FirstOrDefault(x => x.Type == MenuType.DoiTac && x.Area == MenuArea.Home);
                MenuLang mlv = new MenuLang();
                mlv.MenuId = doitac.Id;
                mlv.Name = "ĐỐI TÁC";
                mlv.LanguageCode = "vi-vn";
                db.MenuLangs.AddObject(mlv);
                db.SaveChanges();
                MenuLang mle = new MenuLang();
                mle.MenuId = doitac.Id;
                mle.Name = "PARTNERS";
                mle.LanguageCode = "en-us";
                db.MenuLangs.AddObject(mle);
                db.SaveChanges();
            }
            ViewBag.GioiThieu = gioithieu;
            ViewBag.ThietBi = thietbi;
            ViewBag.DichVu = dichvu;
            //ViewBag.TinTuc = tintuc;
            //ViewBag.LienHe = lienhe;
            ViewBag.DoiTac = doitac;
            ViewBag.mess = mess;
            return View();
        }

        [Authorize(Roles = "Admin,Menu")]
        [HttpPost]
        public ActionResult Index(MenuModel model)
        {
            Menu gioithieu = db.Menus.FirstOrDefault(x => x.Id == model.gtid);
            if (gioithieu != null)
            {
                gioithieu.Icon = model.gticon;
                gioithieu.Image = model.gtimage;
                db.SaveChanges();
            }
            Menu thietbi = db.Menus.FirstOrDefault(x => x.Id == model.tbid);
            if (thietbi != null)
            {
                thietbi.Icon = model.tbicon;
                thietbi.Image = model.tbimage;
                db.SaveChanges();
            }
            Menu dichvu = db.Menus.FirstOrDefault(x => x.Id == model.dvid);
            if (dichvu != null)
            {
                dichvu.Icon = model.dvicon;
                dichvu.Image = model.dvimage;
                db.SaveChanges();
            }
            Menu doitac = db.Menus.FirstOrDefault(x => x.Id == model.dtid);
            if (doitac != null)
            {
                doitac.Icon = model.dticon;
                doitac.Image = model.dtimage;
                db.SaveChanges();
            }
            return RedirectToAction("Index", new { mess = "Lưu dữ liệu thành công!"});
        }

        [ChildActionOnly]
        public ActionResult ListMenuLoadParent()
        {
            string role = "";
            var ac = db.Accounts.FirstOrDefault(x => x.Account1.Equals(User.Identity.Name));
            if (ac != null)
            {
                role = ac.Role;
            }
            var data = cm.GetMenuParrent(MenuArea.Admin,role);
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListMenuLoadChil(int parent)
        {
            var data = cm.GetMenuChil(parent, MenuArea.Admin);
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult CountMenuLoadChil(int parent)
        {
            var data = db.Menus.Where(x => x.Type == MenuArea.Admin && x.Parrent == parent).ToList();
            return PartialView(data.Count);
        }
    }
}
